/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.foment.gem.dialogs;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
//import org.foment.config.*;
import java.net.*;
import org.foment.data.User;
import org.foment.gem.*;
import java.io.*;
import org.foment.util.Md5Generator;

/**
 *
 * @author dusan
 */
public class DusanLoginDialog extends JFrame
{
    // prvo saljemo uneti username i password serveru.
    public DusanLoginDialog(Client cl)
    {
        super("Login to Gem");
        createGUI();
        client = cl;
        this.setSize(300, 150);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setVisible(true);
    }
    private void createGUI()
    {
        l1.setLabelFor(uname);
        l2.setLabelFor(pwd);
        jp.add(l1);
        jp.add(uname);
        jp.add(l2);
        jp.add(pwd);
        jp.add(ok);
        jp.add(cancel);
        this.setContentPane(jp);
        ok.addActionListener(listen);
    }
    ActionListener listen = new ActionListener()
    {

        public void actionPerformed(ActionEvent ae) 
        {
            // ako polje za text nije prazno, saljemo zahtev za logovanjem.
            if (uname.getText().equals(""))
            {
                username = new String(uname.getText());
                password = Md5Generator.md5(new String(pwd.getPassword()));
                System.out.println("Uneto : " + username + password);

/*
                client.send(TransferProtocol.USER_LOGIN, new User(username, password));
                // zatim primamo rezultat nazad.
                try
                {
                    command = client.in.readShort();
                    if (command == TransferProtocol.PASSWORD_CORRECT)
                    {
                        System.out.println("Username and password OK");
                        client.in.readObject();
                        client.start(); // pokrecemo nit, tj. program
                        JOptionPane.showMessageDialog(null, "Login Successful", "Login", JOptionPane.INFORMATION_MESSAGE);
                        dispose(); // gasimo prozor.
                    }
                    else if (command == TransferProtocol.PASSWORD_INCORRECT)
                    {
                        JOptionPane.showMessageDialog(null, "Username or password incorrect", "Login", JOptionPane.INFORMATION_MESSAGE);
                        System.out.println("Username or password incorrect");
                        client.in.readObject();
                    }
                }
                catch(IOException e) {e.printStackTrace();}
                catch(ClassNotFoundException e) {e.printStackTrace();}
 * 
 */
            }

        }
    };
    private short command;
    public String username = null;
    public String password = null;
    private JTextField uname = new JTextField(15);
    private JPasswordField pwd = new JPasswordField(15);
    private JPanel jp = new JPanel(new FlowLayout());
    private JButton ok = new JButton("Login");
    private JButton cancel = new JButton("Cancel");
    private JLabel l1 = new JLabel("Username:");
    private JLabel l2 = new JLabel("Password:");
    private Client client;
}
